package com.cdut.logistics.service.impl.businesses;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.cdut.logistics.config.Constants;
import com.cdut.logistics.config.service;
import com.cdut.logistics.model.dao.businesses.UserDao;
import com.cdut.logistics.model.dao.datacentre.Area;
import com.cdut.logistics.model.dao.datacentre.User;
import com.cdut.logistics.model.entity.Model.Academy;
import com.cdut.logistics.model.entity.Model.Dorm;
import com.cdut.logistics.model.entity.Model.Major;
import com.cdut.logistics.service.interf.businesses.UserService;
import com.jfinal.plugin.activerecord.Db;





/**  
 * @ClassName: s  
 * @Description: TODO(这里用一句话描述这个类的作用)  
 * @author wxt.asus
 * @date 2018年1月26日 上午9:14:21    
 */
@service("userService")	
public class UserServiceImp implements UserService{
	public  int sign(String name,String password,String sex,String phone,int school_id,int academy_id,int major_id,int dorm_id)
    {  
	  User user = UserDao.dao.sign(name, password, sex, phone, school_id, academy_id, major_id, dorm_id);
	  int number;
	  if(user==null)
	  {
		  number =0;
	  }else
	  {
		  number = user.getInt("id");
	  }
   	  return number;
    }
	
	public  boolean query(String name)
    {
   	 
   	  String user_sql = "select * from "+User.table+" where name =? and "+Constants.sql_add;
         User user= User.dao.findFirst(user_sql,name);
         
        
         
         if(user !=null)
         {
       	  return true;
         }
         return false; 
    }
	
	public  boolean query(String name,String password)
    {
   	 
		//dao 返回一个User
   	 String user_sql = "select * from "+User.table+" where active =1 and is_use =1 and name = ? and password = ?";
   	 User user= User.dao.findFirst(user_sql,name,password);
   	 //
   	 
   	 if(user !=null)
   	 {
   		 return true;
   	 } 
   	  return false; 
    }
	/**
	 * 
	 */
	 public  List<Academy> Search_Academy(int school_id)
	 {
		 String user_sql = "select name,id from "+Academy.table+" where   school_id =? and "+Constants.sql_add;
   	     List<Academy>  list = Academy.dao.find(user_sql,school_id);  	     
   	     return list;   
	 }
	 
	 public List<Major>  Search_Major(int school_id)
	 {
		 String user_sql = "select name,id from "+Major.table+" where   school_id =? and "+Constants.sql_add;
   	     List<Major>  list = Major.dao.find(user_sql,school_id);  	     
   	     return list;  
	 }
	 
	 public  List<Area> Search_Area(int school_id)
	 {
		 String user_sql = "select name,id from "+Area.table+" where   school_id =? and "+Constants.sql_add;
   	     List<Area>  list = Area.dao.find(user_sql,school_id);  	     
   	     return list; 
	 }
	 
	 public List<Dorm> Search_Dorm(int school_id, int area_id)
	 {
		 String user_sql = "select name,id from "+Dorm.table+" where   school_id =? and area_id = ? and "+Constants.sql_add;
   	     List<Dorm>  list = Dorm.dao.find(user_sql,school_id,area_id);  	     
   	     return list;
	 }

	@Override
	public void addAccess_token(String access_token,String name) {
		System.out.println("ssssssssssssssss");
		Db.update("update user set access_token=? where name = ? ",access_token,name);

	}

	
}
